package com.znyj.servlet.sqlHelper;

import java.sql.Timestamp;
import java.util.Iterator;
import java.util.List;

/**
 * Created by nameless on 15-2-12.
 */
public class SqlBuildHelper {

    public static String makeWheres(List<Where> wheres) {
        StringBuilder builder = new StringBuilder();
        builder.append(" WHERE");

        Iterator<Where> iterator = wheres.iterator();

        while (iterator.hasNext()) {
            Where where = iterator.next();
            if (where.isIgnore()) {
                builder.append(" 1=1");
            } else {
                builder.append(" ");
                builder.append(makeWhere(where));
            }

            if (iterator.hasNext()) {
                builder.append(where.getConjunction().toString());
            }
        }

        return builder.toString();
    }

//    public static String makeWheres(List<Where> wheres) {
//        StringBuilder builder = new StringBuilder();
//        builder.append(" WHERE");
//
//        for (int loop=0;loop<wheres.size();loop++)
//        {
//            Where where = wheres.get(loop);
//            if (where.isIgnore()) {
//                builder.append(" 1=1");
//            } else {
//                builder.append(" ");
//                builder.append(makeWhere(where));
//            }
//
//            if(loop == (wheres.size()-1))
//            {
//                builder.append(" ");
//
//            }
//            else
//            {
//                builder.append(where.getConjunction().toString());
//            }
//        }
//
//        return builder.toString();
//    }


    private static String makeWhere(Where where) {
        Where.Judge judge = where.getJudge();
        //将来如果有特殊情况，可以用if进行拦截特殊处理
        if(judge.equals(Where.Judge.CAST))
            return (String)where.getValue();
        return where.getFieldName() + judge.toString() + sqlValue(where.getValue());
    }

    private static String sqlValue (Object value) {
        if (value instanceof String) {
            return "'" + value + "'";
        }

        if (value instanceof Iterator) {
            StringBuilder builder = new StringBuilder();
            builder.append("(");
            Iterator iterator = (Iterator) value;
            while (iterator.hasNext()) {
                builder.append(iterator.next());
                if (iterator.hasNext()) {
                    builder.append(",");
                }
            }
            builder.append(")");
            return builder.toString();
        }

        if(value instanceof Timestamp) {
            return "'" + String.valueOf(value) + "'";
        }

        return String.valueOf(value);
    }

    public static String makeLimit(Limit limit) {
        if (limit.isSkip()) {
            return " LIMIT " + limit.getSkip() + "," + limit.getNumber();
        }else {
            return " LIMIT " + limit.getNumber();
        }
    }

    public static String makeOrders(List<Order> orders) {
        StringBuilder builder = new StringBuilder();
        builder.append(" ORDER BY ");
        Iterator<Order> orderIterator = orders.iterator();
        while (orderIterator.hasNext()) {
            Order order = orderIterator.next();
            builder.append(order.getFieldName());
            builder.append(" ");
            builder.append(order.getSort().getValue());
            if (orderIterator.hasNext()) {
                builder.append(",");
            }
        }
        return builder.toString();
    }


}
